package primaryServer

import (
	"math/rand"
	"time"

	"golang.org/x/exp/slices"
)

// initial game state
type PlayerInfo struct {
	score    int
	location Position
}

type Position struct {
	x int
	y int
}

type GameState struct {
	PlayerInfos   map[string]PlayerInfo
	Treasures     []Position
	StartTime     string
	PrimaryServer string
	BackupServer  string
}

func initGameState(id string, K int, N int) GameState {
	playerInfos := make(map[string]PlayerInfo)
	playerInfos[id] = PlayerInfo{
		score:    0,
		location: Position{0, 0},
	}

	treasures := make([]Position, K)
	i := 0
	for i < K {
		x := rand.Intn(N)
		y := rand.Intn(N)
		if slices.Contains(treasures, Position{x, y}) {
			continue
		}
		treasures[i] = Position{x, y}
		i++
	}

	initialGameState := GameState{
		StartTime:     time.Now().Format("15:04:05"),
		PlayerInfos:   playerInfos,
		Treasures:     treasures,
		PrimaryServer: id,
	}
	return initialGameState
}
